package com.handpay.zztong.hp.bean;

import android.text.TextUtils;

import com.handpay.framework.ClientEngine;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;

public class Address {
	private static final String TAG = "Address";
	public static double locationLat;
	public static double locationLng;
	public static double areaRadius;
    public static boolean IS_CHECK_ADDRESS = false;

	
	public static void reSetAddress(){
		areaRadius  = 30.0;
//		locationLat = 44.219147;
//		locationLng = 80.41216;
		double lat = (Double) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_TEST_SELECT_LATITUDE, 0d);
		double lng = (Double) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_TEST_SELECT_LONGTITUDE, 0d);
		if(lat == 0 || lng == 0){
			locationLat = 31.202515;
			locationLng = 121.488975;
		}else{
			locationLat = lat;
			locationLng = lng;
		}
		
		HPLog.e(TAG, "reSetAddress--locationLng="+locationLng+"-------locationLat="+locationLat);
	}
	public static void setAddress(double lat,double lng,double radius){
		locationLat = lat;
		locationLng = lng;
		areaRadius  = radius;
		HPLog.e(TAG, "setAddress--locationLng="+locationLng+"-------locationLat="+locationLat +"---radius"+radius);
	}
	
	public static boolean checkAddressIsSuccess(){
		HPLog.i(TAG, "checkAddressIsSuccess-------");
		if(IS_CHECK_ADDRESS){
			String lat = BaseProtocolKey.KEY_LATITUDE;
			String loat = BaseProtocolKey.KEY_LONGTITUDE;
			String adds = BaseProtocolKey.KEY_ADDRESS;
			if (ZZTConfig.isTestLocation) {
				lat = BaseProtocolKey.KEY_TEST_LATITUDE;
				loat = BaseProtocolKey.KEY_TEST_LONGTITUDE;
				adds = BaseProtocolKey.KEY_TEST_ADDRESS;
			}
			Double latitude = (Double) ClientEngine.getInstance().getGlobal(lat, 0d);
			Double longtitude = (Double) ClientEngine.getInstance().getGlobal(loat, 0d);
			String add = (String) ClientEngine.getInstance().getGlobal(adds);
			HPLog.i(TAG, "本地地址--longtitude="+longtitude+"---latitude="+latitude);
			HPLog.i(TAG, "后台限制--locationLng="+locationLng+"---locationLat="+locationLat + "---areaRadius="+areaRadius);
			if (0d != latitude && 0d != longtitude && !TextUtils.isEmpty(add)) {
				if(getDistance(locationLng, locationLat, longtitude, latitude) <= areaRadius){
					return true;
				}
			} else {
				HPLog.i(TAG, "no address!");
			}
		}
		return false;
	}
	
	public static double getDistance(double LonA, double LatA, double LonB, double LatB)  
	{  
	    // 东西经，南北纬处理，只在国内可以不处理(假设都是北半球，南半球只有澳洲具有应用意义)  
	    double MLonA = LonA;  
	    double MLatA = LatA;  
	    double MLonB = LonB;  
	    double MLatB = LatB;  
	    // 地球半径（千米）  
	    double R = 6371.004;
	    double C = Math.sin(rad(LatA)) * Math.sin(rad(LatB)) + Math.cos(rad(LatA)) * Math.cos(rad(LatB)) * Math.cos(rad(MLonA - MLonB));  
	    return (R * Math.acos(C));  
	}  
	  
	private static double rad(double d)  
	{  
	    return d * Math.PI / 180.0;  
	}  
}
